﻿using System;
using System.Collections.Generic;
using System.Configuration;
using System.IO;
using System.Text;
using System.Xml;

namespace Yywl.Svc.Core
{
    /// <summary>
    /// 配置类
    /// </summary>
    public class ConfigHelper
    {
        #region 基本配置信息
        /// <summary>
        /// 是否打开生成的检索数据目录
        /// </summary>
        public static string EnabledIndexData_OpenDir = ConfigurationManager.AppSettings["EnabledIndexData_OpenDir"];


        /// <summary>
        /// 平台访问地址
        /// </summary>
        public static string DoaminURL = ConfigurationManager.AppSettings["DoaminURL"];


        #endregion

        #region 读取配置文件
        /// <summary>
        /// 读取配置文件
        /// </summary>
        /// <param name="key"></param>
        /// <returns></returns>
        public static string GetValue(string key)
        {
            return ConfigurationManager.AppSettings[key];
        }
        /// <summary>
        /// 获取整型配置值
        /// </summary>
        /// <param name="key"></param>
        /// <returns></returns>
        public static int GetIntValue(string key)
        {
            return Convert.ToInt32(ConfigurationManager.AppSettings[key].ToString());
        }
        #endregion


        #region 获取链接字符串
        /// <summary>
        /// 获取链接字符串
        /// </summary>
        /// <param name="key"></param>
        /// <returns></returns>
        public static string GetConnectionStrings(string key)
        {
            return ConfigurationManager.ConnectionStrings[key].ToString();
        }
        #endregion

        #region 保存键值对信息
        /// <summary>
        /// 保存配置
        /// </summary>
        /// <param name="appKey"></param>
        /// <param name="appValue"></param>
        /// <returns></returns>

        public static bool SaveConfig(string appKey, string appValue)
        {
            try
            {
                string configPath = System.Windows.Forms.Application.StartupPath +
            "\\"
            + "PDF_Tools.exe.config";
                XmlDocument xDoc = new XmlDocument();
                xDoc.Load(configPath);
                XmlNode xNode;
                XmlElement xmlElement01;
                XmlElement xmlElement02;
                xNode = (XmlElement)xDoc.SelectSingleNode("//appSettings");
                xmlElement01 = (XmlElement)xNode.SelectSingleNode(string.Format("//add[@key='{0}']", appKey));
                if (xmlElement01 != null)
                {
                    xmlElement01.SetAttribute("value", appValue);
                }
                else
                {
                    xmlElement02 = xDoc.CreateElement("add");
                    xmlElement02.SetAttribute("key", appKey);
                    xmlElement02.SetAttributeNode("value", appValue);

                }
                xDoc.Save(configPath);
                return true;
            }
            catch (Exception exception)
            {
                return false;
            }


        }
        #endregion

        #region Json格式的配置信息
        /// <summary>
        /// 保存json的配置
        /// </summary>
        /// <returns></returns>
        public static bool SaveJsonConfig(string filePath, string json)
        {
            try
            {
                using (FileStream fs = new FileStream(filePath, FileMode.OpenOrCreate))
                {
                    byte[] data = System.Text.Encoding.UTF8.GetBytes(json);
                    fs.Write(data, 0, data.Length);
                    fs.Flush();
                    fs.Close();
                }

            }
            catch (Exception)
            {

                return false;
            }

            return true;

        }
        /// <summary>
        /// 读取json配置文件信息
        /// </summary>
        /// <param name="filePath"></param>
        /// <returns></returns>
        public static string GetJsonConfig(string filePath)
        {
            string json = string.Empty;
            try
            {
                json = File.ReadAllText(filePath).Trim();
            }
            catch (Exception)
            {
                return string.Empty;
            }
            return json;
        }
        #endregion

        #region 字符串读取
        /// <summary>
        /// 读取字符串的配置项
        /// </summary>
        /// <param name="key"></param>
        /// <returns></returns>
        public static string GetAppKey_String(string key)
        {
            return ConfigurationManager.AppSettings[key];
        }

        /// <summary>
        /// 读取字符串的配置项2
        /// </summary>
        /// <param name="key"></param>
        /// <returns></returns>
        public static string GetAppConfig(string key)
        {
            return ConfigurationManager.AppSettings[key];
        }


        /// <summary>
        /// 读取整型的配置项
        /// </summary>
        /// <param name="key"></param>
        /// <returns></returns>
        public static int GetAppConfigToInt32(string key)
        {
            return Convert.ToInt32(ConfigurationManager.AppSettings[key].ToString());
        }
        #endregion

        #region 模型训练cmd配置项（训练）
        /// <summary>
        /// 短期模型训练按钮【加药】
        /// </summary>
        public static string start_short_train = ConfigurationManager.AppSettings["start_short_train"];

        /// <summary>
        /// 中期模型训练按钮【加药】
        /// </summary>
        public static string start_middle_train = ConfigurationManager.AppSettings["start_middle_train"];
        /// <summary>
        /// 长期模型训练按钮【加药】
        /// </summary>
        public static string start_long_train = ConfigurationManager.AppSettings["start_long_train"];
        /// <summary>
        /// 停止 -短期【加药】
        /// </summary>
        public static string stop_short_train = ConfigurationManager.AppSettings["stop_short_train"];
        /// <summary>
        /// 停止 -中期【加药】
        /// </summary>
        public static string stop_middle_train = ConfigurationManager.AppSettings["stop_middle_train"];
        /// <summary>
        /// 停止 -长期【加药】
        /// </summary>
        public static string stop_long_train = ConfigurationManager.AppSettings["stop_long_train"];
        #endregion

        #region 模型预测cmd配置项（预测）
        /// <summary>
        /// 短期模型预测按钮
        /// </summary>
        public static string start_short_predict = ConfigurationManager.AppSettings["start_short_predict"];

        /// <summary>
        /// 中期模型预测按钮
        /// </summary>
        public static string start_middle_predict = ConfigurationManager.AppSettings["start_middle_predict"];
        /// <summary>
        /// 长期模型预测按钮
        /// </summary>
        public static string start_long_predict = ConfigurationManager.AppSettings["start_long_predict"];
        /// <summary>
        /// 停止 -短期
        /// </summary>
        public static string stop_short_predict = ConfigurationManager.AppSettings["stop_short_predict"];
        /// <summary>
        /// 停止 -中期
        /// </summary>
        public static string stop_middle_predict = ConfigurationManager.AppSettings["stop_middle_predict"];
        /// <summary>
        /// 停止 -长期
        /// </summary>
        public static string stop_long_predict = ConfigurationManager.AppSettings["stop_long_predict"];
        #endregion


        #region 模型训练cmd配置项（训练加氯）
        /// <summary>
        /// 短期模型训练按钮
        /// </summary>
        public static string start_short_train_jialv = ConfigurationManager.AppSettings["start_short_train_jialv"];

        /// <summary>
        /// 中期模型训练按钮
        /// </summary>
        public static string start_middle_train_jialv = ConfigurationManager.AppSettings["start_middle_train_jialv"];
        /// <summary>
        /// 长期模型训练按钮
        /// </summary>
        public static string start_long_train_jialv = ConfigurationManager.AppSettings["start_long_train_jialv"];
        /// <summary>
        /// 停止 -短期
        /// </summary>
        public static string stop_short_train_jialv = ConfigurationManager.AppSettings["stop_short_train_jialv"];
        /// <summary>
        /// 停止 -中期
        /// </summary>
        public static string stop_middle_train_jialv = ConfigurationManager.AppSettings["stop_middle_train_jialv"];
        /// <summary>
        /// 停止 -长期
        /// </summary>
        public static string stop_long_train_jialv = ConfigurationManager.AppSettings["stop_long_train_jialv"];
        #endregion

        #region 模型预测cmd配置项（预测加氯）
        /// <summary>
        /// 短期模型预测按钮
        /// </summary>
        public static string start_short_predict_jialv = ConfigurationManager.AppSettings["start_short_predict_jialv"];

        /// <summary>
        /// 中期模型预测按钮
        /// </summary>
        public static string start_middle_predict_jialv = ConfigurationManager.AppSettings["start_middle_predict_jialv"];
        /// <summary>
        /// 长期模型预测按钮
        /// </summary>
        public static string start_long_predict_jialv = ConfigurationManager.AppSettings["start_long_predict_jialv"];
        /// <summary>
        /// 停止 -短期
        /// </summary>
        public static string stop_short_predict_jialv = ConfigurationManager.AppSettings["stop_short_predict_jialv"];
        /// <summary>
        /// 停止 -中期
        /// </summary>
        public static string stop_middle_predict_jialv = ConfigurationManager.AppSettings["stop_middle_predict_jialv"];
        /// <summary>
        /// 停止 -长期
        /// </summary>
        public static string stop_long_predict_jialv = ConfigurationManager.AppSettings["stop_long_predict_jialv"];
        #endregion
        /// <summary>
        /// 模型训练等windows服务配置文件夹
        /// </summary>
        public static string ModelServiceConfigDir = ConfigurationManager.AppSettings["ModelServiceConfigDir"];

        #region 加药系统，参数配置范围
        /// <summary>
        /// 范围-加药系统输入
        /// </summary>
        public static string range_jiayao_input = ConfigurationManager.AppSettings["range_jiayao_input"];
        /// <summary>
        /// 范围-加药系统输出
        /// </summary>
        public static string range_jiayao_output = ConfigurationManager.AppSettings["range_jiayao_output"];
        /// <summary>
        /// 范围-加药预测输出
        /// </summary>
        public static string range_jiayao_predictOutput = ConfigurationManager.AppSettings["range_jiayao_predictOutput"];
        /// <summary>
        /// 范围-加药中间变量
        /// </summary>
        public static string range_jiayao_tmpVariable = ConfigurationManager.AppSettings["range_jiayao_tmpVariable"];

        #endregion


        #region 加药系统，参数配置范围(加药配比模型)
        /// <summary>
        /// 范围-加药系统输入(加药配比模型)
        /// </summary>
        public static string range_jiayao_input_peibi = ConfigurationManager.AppSettings["range_jiayao_input_peibi"];
        /// <summary>
        /// 范围-加药系统输出(加药配比模型)
        /// </summary>
        public static string range_jiayao_output_peibi = ConfigurationManager.AppSettings["range_jiayao_output_peibi"];
        /// <summary>
        /// 范围-加药预测输出(加药配比模型)
        /// </summary>
        public static string range_jiayao_predictOutput_peibi = ConfigurationManager.AppSettings["range_jiayao_predictOutput_peibi"];
        /// <summary>
        /// 范围-加药中间变量(加药配比模型)
        /// </summary>
        public static string range_jiayao_tmpVariable_peibi = ConfigurationManager.AppSettings["range_jiayao_tmpVariable_peibi"];

        #endregion

        #region 加氯系统，参数配置范围
        /// <summary>
        /// 范围-加氯系统输入
        /// </summary>
        public static string range_jialv_input = ConfigurationManager.AppSettings["range_jialv_input"];
        /// <summary>
        /// 范围-加氯系统输入
        /// </summary>
        public static string range_jialv_output = ConfigurationManager.AppSettings["range_jialv_output"];
        /// <summary>
        /// 范围-加氯预测输出
        /// </summary>
        public static string range_jialv_predictOutput = ConfigurationManager.AppSettings["range_jialv_predictOutput"];
        /// <summary>
        /// 范围-加氯中间变量
        /// </summary>
        public static string range_jialv_tmpVariable = ConfigurationManager.AppSettings["range_jialv_tmpVariable"];

        #endregion


        /// <summary>
        /// 用户离线状态（多角色用户控制），时间设置，单位：小时，默认设置为24小时
        /// </summary>
        public static int LoginExpiredHour = Convert.ToInt32(ConfigurationManager.AppSettings["LoginExpiredHour"]);

        /// <summary>
        /// 进水量公式，加药
        /// </summary>
        public static string WaterInflowFormula_jiayao = ConfigurationManager.AppSettings["WaterInflowFormula_jiayao"];
        /// <summary>
        /// 进水量公式，加氯
        /// </summary>
        public static string WaterInflowFormula_jialv = ConfigurationManager.AppSettings["WaterInflowFormula_jialv"];
    }
}
